﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UMLTouch.Core.Entities;
using UMLTouch.Domain.ClassDiagram.Items;
using UMLTouch.Core.UI;

namespace UMLTouch.Domain.ClassDiagram.Patterns
{
    public class RelationArrow : IShapePattern
    {
        #region IShapePattern Members

        public ShapePatternMatchingResultEnum Match(Shape shape)
        {
            Shape shapeToAnalyse = shape;
            switch (shape.ShapeOpenType)
            {
                case ShapeOpenTypeEnum.Closed:
                    break;
                case ShapeOpenTypeEnum.Opened:

                    return ShapePatternMatchingResultEnum.MightMatch;
                case ShapeOpenTypeEnum.SemiClosed:
                    shapeToAnalyse = shape.SubShape;
                    break;
            }

            if (shapeToAnalyse.NbSegments < 3)
            {
                return ShapePatternMatchingResultEnum.MightMatch;
            }
            else if (shapeToAnalyse.NbSegments == 3
                && shapeToAnalyse.NbExtremityIntersections == 1
                && shapeToAnalyse.NbMiddleIntersections == 0)
            {
                return ShapePatternMatchingResultEnum.Match;
            }


            return ShapePatternMatchingResultEnum.DontMatch; 

        }

        public RelationArrow(String name)
        {
            Name = name;
        }

        public string Name
        {
            get;
            set;
        }

        #endregion

        #region IEquatable<IShapePattern> Members

        public bool Equals(IShapePattern other)
        {
            return Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase);
        }

        #endregion

        #region IShapePattern Members


        public IDiagramItem GetItem(Shape shape)
        {
            return new RelationArrowItem(shape);
        }

        #endregion
    }
}
